<?php
/**
 * Created by PhpStorm.
 * User: fengyan
 * Date: 2017/5/9
 * Time: 下午11:59
 */

namespace Home\Controller;


use Think\View;

class PageController extends HomeController
{
    /**
     * @param $cateid
     */
    public function detail($cateid)
    {
        //  分类信息
        $categoryInfo = D('Category')->info($cateid);

        $childCategoryList = D('Category')->getListByPid($categoryInfo['id']);
        if ($childCategoryList) {
            $categoryInfo = $childCategoryList[0];
        }
        $cateid = $categoryInfo['id'];

        //  同一级分类的列表
        $categoryList = D('Category')->getListByPid($categoryInfo['pid']);

        //  同一级分类的上级分类列表
        $parentCategoryInfo = D('Category')->info($categoryInfo['pid']);
        $rootid = $parentCategoryInfo['pid'] ? $parentCategoryInfo['pid'] : 0;
        if (empty($rootid)) {
            $parentCategoryList = $categoryList;
            $parentCategoryInfo = $categoryInfo;
            $categoryList = [];
        } else {
            $parentCategoryList = D('Category')->getListByPid($rootid);
        }

        $this->assign('categoryInfo', $categoryInfo);
        $this->assign('categoryList', $categoryList);
        $this->assign('parentCategoryList', $parentCategoryList);
        $this->assign('parentCategoryInfo', $parentCategoryInfo);

        if (!empty($categoryInfo['meta_title'])) {
            $this->seo['title'] = $categoryInfo['meta_title'];
        }

        if (!empty($categoryInfo['keywords'])) {
            $this->seo['keywords'] = $categoryInfo['keywords'];
        }

        if (!empty($categoryInfo['description'])) {
            $this->seo['description'] = $categoryInfo['description'];
        }

        $this->assign('seo', $this->seo);

        $detail = D('Page')->find($cateid);

        $this->assign('detail', $detail);

        $template = $categoryInfo['template_detail'];
        $view = new View();
        if(!is_file($view->parseTemplate($template))) {
            $template = 'About/detail';
        }

        //  获取广告

        if ($categoryInfo['pid'] == 0) {
            $rootCategoryInfo = $categoryInfo;
        } else {
            $parentCategoryInfo = D('Category')->info($categoryInfo['pid']);
            if($parentCategoryInfo['pid'] == 0) {
                $rootCategoryInfo = $parentCategoryInfo;
            } else {
                $rootCategoryInfo = D('Category')->info($parentCategoryInfo['pid']);
            }
        }
        $this->assign('bannerList', D('Advertise')->getAdvertiseListByOnlySign(strtolower($rootCategoryInfo['name'])));

        $this->display($template);
    }
}